﻿using SharpDepend;
using SharpDepend.Datatypes;
using ZquenceStudio3Host.Graphics.Components;
using ZquenceStudio3Host.Graphics.GUI;

namespace ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.GUI.Controls
{
    public class ValueEditor : Panel
    {
        private Label lblEditParameter;
        public Textbox textbox;
        private bool mFirst;

        public bool IsValid
        {
            get
            {
                return textbox.IsValid;
            }
            set
            {
                textbox.IsValid = value;
            }
        }

        // Public events.
        public event EventOnTextChanged OnTextChanged;

        public ValueEditor(BasicGuiManager engineCore, string name)
            : base(engineCore, true)
        {
            BackgroundColor = Color.Transparent;

            lblEditParameter = new Label(engineCore, true);
            lblEditParameter.X = 0;
            lblEditParameter.Y = 0;
            lblEditParameter.Text = name;
            AddPart(lblEditParameter);

            textbox = new Textbox(engineCore, true);
            textbox.X = 0;
            textbox.Height = 20;
            textbox.OnTextChanged += Textbox_OnTextChanged;
            textbox.BackgroundColor = Color.Black;
            AddPart(textbox);
        }

        private void Textbox_OnTextChanged(string text)
        {
            if (mFirst)
            {
                TextChanged(text);
                OnTextChanged?.Invoke(text);
            }
            mFirst = true;
        }

        protected virtual void TextChanged(string newText)
        {

        }

        public string Text
        {
            get { return textbox.GetText(); }
            set { textbox.SetText(value); }
        }

        public bool AutoSize
        {
            get { return textbox.AutoSize; }
            set { textbox.AutoSize = value; }
        }

        public double TextSize
        {
            get { return textbox.TextSize; }
            set { textbox.TextSize = value; }
        }

        public string TitleHint { get { return textbox.TitleHint; } set { textbox.TitleHint = value; }  }

        public string DescriptionHint { get { return textbox.DescriptionHint; } set { textbox.DescriptionHint = value; } }

        public override void Render(IGraphicsDevice canvas, Parent parent)
        {
            textbox.Y = lblEditParameter.Height;
            textbox.Width = parent.Width;

            base.Render(canvas, parent);
        }
    }
}
